package cn.demoncat.util.web;

import org.apache.commons.lang3.StringUtils;

import cn.demoncat.util.lang.StringUtil;

/**
 * Html工具
 * 
 * @author 延晓磊
 *
 * @since 2019年7月21日
 */
public class HtmlUtil {

	
	// 段落
	public static final String PLL = "<p class='";
	public static final String PLR = "'>";
	public static final String PR = "</p>";
	// 转义 < > 
	public static final String LT = "<";
	public static final String RT = ">";
	public static final String HLT = "&lt;";
	public static final String HRT = "&gt;";
	// 转义空格
	public static final String S = " ";
	public static final String HS = "&nbsp;";
	// 转义&
	public static final String AD = "&";
	public static final String HAD = "&amp;";
	// 转义回车换行
	public static final String RN = "\r\n";
	public static final String N = "\n";
	public static final String HN = "</br>";
	
	
	/**
	 * Html转义
	 * 
	 * @param line
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2019年7月21日
	 */
	public static String trans(String line){
		if (StringUtils.isNotBlank(line)) {
			// 转义&
			line = StringUtil.replace(line, AD, HAD);
			// 转义 <
			line = StringUtil.replace(line, LT, HLT);
			// 转义 >
			line = StringUtil.replace(line, RT, HRT);
			// 转义空格
			line = StringUtil.replace(line, S, HS);
			// 转义换行
			line = StringUtil.replace(line, RN, HN);
			line = StringUtil.replace(line, N, HN);
		}
		return line;
	}
	
	/**
	 * 转换行数据为段落
	 * 
	 * @param line	行数据
	 * @param clazz	段落标签的class
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2019年7月21日
	 */
	public static String transParagraph(String line, String clazz) {
		// 转义line
		line = HtmlUtil.trans(line);
		// 拼接html
		return PLL + clazz + PLR + line + PR;
	}
	
}
